/**
 * Copyright 2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.flest.application
{
 /**
 * @author		Ivan Petrov
 * @date		14/05/2007
 * @version		2.0
 * @purpose		Application class - refers to Controller.
*/
	import mx.core.Application;
	import mx.states.State;
	import flash.events.Event;
	import org.flest.command.ICommand;
	import org.flest.controller.FlestController;
	import org.flest.util.ToolBox;
	import org.flest.constant.Errors;
	import mx.collections.ArrayCollection;
	import com.ericfeminella.utils.TestRun;
	import org.flest.model.ModelLocator;
	import org.flest.constant.FlestKey;

	public class FlestApplication extends Application
	{
		public var errorStateName:String;
		
		public function FlestApplication()
		{
			super();
			FlestController.getInstance().setApp(this);
		}
	
		/**
		 * A handle to any updates before the User begins the work.
		 */
		protected function init():void
		{
			//set error state name in the model
			if(!ToolBox.isStringEmpty(this.errorStateName)) {
				this.setErrorStateName();
			}
		}
		
		private function setErrorStateName():void
		{
			//check if such name exists in the application
			for(var i:int=0; i<this.states.length; i++) {
				var stateName:String = State(this.states[i]).name;
				if(this.errorStateName == stateName) {
					ModelLocator.getInstance().addValue(FlestKey.ERROR_STATE_NAME, this.errorStateName);
					return;
				}
			}
			//throw an error if there is no such name
			if(ToolBox.isStringEmpty(errorStateName)) {
				throw new Error(Errors.MSG_INVALID_STATE_NAME);
			}
		}
		
		public function exec(func:Function, args:Array):void 
		{
			FlestController.getInstance().exec(func, args);
		}
	}
}
